#include <iostream>
#include <pthread.h>
#include <cassert>
#include <unistd.h>

using namespace std;

int g_val = 0;
string func()
{
    return " 独立的方法 ";
}
// 新线程
void* thread_routine(void* args)
{
    const char* name = (const char*)args;
 while(true)
    {
        cout << "我是新线程，正在运行 name : " << name  << func() << "-"<<g_val++ << ": &g_val :"<< &g_val<<   endl;
        sleep(1);
    }
}
int main()
{
    pthread_t tid;
    int n = pthread_create(&tid,nullptr,thread_routine,(void*)"thread one");
    assert(0 == n);
    (void)n;

    char namebuffer[64];
    snprintf(namebuffer,sizeof(namebuffer),"0x%x",tid);

    while(true)
    {
        cout << "我是主线程，正在运行 tid : " << namebuffer<< func() << "-"<<g_val << ": &g_val :"<< &g_val <<  endl;
        sleep(1);
    }
    return 0;
}